package pure.manager
{
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import mx.controls.Alert;
	
	import pure.DesB;
	import pure.core.Map;
	import pure.events.MapEvent;

public class MapManager extends EventDispatcher
{
	
	public var mapList:Dictionary=new Dictionary();

	public var currMap:Map;
	
	public var deepBuffer:BitmapData;
	public var shallowBuffer:BitmapData;
	
	
	// 生成新地图
	public function createNewMap(tileSize:Number,numTileX:int,numTileY:int,numTileInSheet:int):void{
		// 检查数据
		if(tileSize<=0||numTileX<=0||numTileY<=0||numTileInSheet<=0||numTileInSheet>32){
			Alert.show("输入有误，请查验","数据错误");
		}
		
		var map:Map=new Map();
		map.tileSize=tileSize;
		map.numTile=new Point(numTileX,numTileY);
		map.numTileInsheet=numTileInSheet;
		currMap=map;
		
		resetDeepRender();
		resetShallowRender();
		resetLandformData();
	}
	
	// 清零深层渲染
	public function resetDeepRender():void{
		currMap.deepRenderData=new Array(currMap.numTile.y);
		for(var i:int=0;i<currMap.numTile.y;i++){
			currMap.deepRenderData[i]=new Array(currMap.numTile.x);
			
			// 初始默认为-1
			for(var k:int=0;k<currMap.numTile.x;k++){
				currMap.deepRenderData[i][k]=-1;
			}
		}
	}
	
	// 清零浅层渲染
	public function resetShallowRender():void{
		currMap.shallowRenderData=new Array(currMap.numTile.y);
		for(var i:int=0;i<currMap.numTile.y;i++){
			currMap.shallowRenderData[i]=new Array(currMap.numTile.x);
			
			// 初始默认为-1
			for(var k:int=0;k<currMap.numTile.x;k++){
				currMap.shallowRenderData[i][k]=-1;
			}
		}
	}
	
	// 清零地形数据
	public function resetLandformData():void{
		currMap.landformData=new Array(currMap.numTile.y);
		for(var i:int=0;i<currMap.numTile.y;i++){
			currMap.landformData[i]=new Array(currMap.numTile.x);
			
			// 初始默认为0
			for(var k:int=0;k<currMap.numTile.x;k++){
				currMap.landformData[i][k]=0;
			}
		}
	}
	
	// 更新深层渲染
	public function updateDeepRender():void{
		_advanceRender(deepBuffer,currMap.deepRenderData);
	}
	
	// 更新浅层渲染
	public function updateShallowRender():void{
		_advanceRender(shallowBuffer,currMap.shallowRenderData);
	}
	
	// 更新渲染（internal）
	private function _advanceRender(buffer:BitmapData,renderData:Array):void{
		buffer.lock();
		
		var rect:Rectangle=new Rectangle(0,0,buffer.width,buffer.height);
		buffer.fillRect(rect,0x0);
		
		var tileSize:Number=currMap.tileSize;

		var lenA:int=renderData.length;
		var lenB:int=renderData[0].length;
		for(var i:int=0;i<lenA;i++){
			for(var j:int=0;j<lenB;j++){
				var index:int=renderData[i][j];
				if(index!=-1){
					var point:Point=new Point(j*tileSize,i*tileSize);
					var tmpX:int=index%currMap.numTileInsheet;
					var tmpY:int=int(index/currMap.numTileInsheet);
					rect=new Rectangle(tmpX*tileSize,tmpY*tileSize,tileSize,tileSize);
					buffer.copyPixels(currMap.sheet,rect,point,null,null,true);
				}
			}
		}
		buffer.unlock();
	}
	
	// 是否已建立地图
	public function get exists():Boolean{return currMap!=null;}
	
	// 当前地图是否已导入Sheet
	public function get sheetImported():Boolean{return currMap.sheet!=null&&currMap.sheet.width!=0&&currMap.height!=0;}
	
	
	// 单例
	public static function get instance():MapManager {
		if (!_instance) _instance = new MapManager();
		return _instance;
	}
	private static var _instance:MapManager;
	
}
}